<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 *奖励
 *@author zhu
 *@time 20150801
 */
class User_kf_model extends CI_Model {
	function __construct(){
        parent::__construct();
    }
    
    function add($data = array()){
		$this->db->insert('rsh_user_kf', $data);
		return $this->db->affected_rows();
    }
    
    
    function get_one($where = array(),$select='*'){
		$this->db->select($select);
		if($where){
			$this->db->where($where);
		}
		$this->db->limit(1);
		$query = $this->db->get('rsh_user_kf');
		return $query->row_array();
    }	

    //统计客服的用户数
    function countUser($kf_id){
    	if(!$kf_id){
    		echo '参数错误，请联系管理员';
            exit;
    	}
    	$sql='select * from rsh_user_kf where kf_id='.$kf_id;
    	$query = $this->db->query($sql);
        $data=$query->num_rows();
        $data=intval($data);
    	return  $data;
    }	  

    //统计客服的被点击的次数
    function click_num($kf_id){
        if(!$kf_id){
            echo '参数错误，请联系管理员';
            exit;
        }    
        $this->db->select_sum('click_num');
        $this->db->where('kf_id',$kf_id);
        $query = $this->db->get('rsh_user_kf');
        //var_dump($query->row());
        return  $query->row()->click_num;
    } 

    //查询客服下所有用户
    function kefuuserlist($kf_id){
    	if(!$kf_id){
    		echo '参数错误，请联系管理员';
            exit;
    	}
    	$sql='select * from rsh_user_kf where kf_id='.$kf_id;
    	$query = $this->db->query($sql);

    	return  $query->result_array();
    }

    //记录用户咨询专属客服点击数
    function user_click($user_id){
        if(!$user_id){
            echo '参数错误，请联系管理员';
            exit;
        }
        $result=$this->get_one(array('userid'=>$user_id));
        if($result){
            $result['click_num']=$result['click_num']+1;
            $sql="update rsh_user_kf set click_num=".$result['click_num']." where userid=".$user_id;
            $query=$this->db->query($sql);            
        }else{
            echo '该用户没有设置专属客服';
            exit;
        }    
        return $this->db->affected_rows();    
    }
}

